<?php
class Core_Library_Download{
    public function __conctruct(){
        
    }
    function resize_image($image_from, $image_to, $width, $height) {
        var_dump($image_from);
    //$image_vars = getimagesize($image_from['name']['tmp_name']);
    $src_width = 300;
    $src_height = 300;
    $src_type = IMAGETYPE_JPEG;

    if($width > $src_width)     $width = $src_width;
    if($height > $src_height)   $height = $src_height;

    switch ($src_type) {
        case IMAGETYPE_JPEG:
            $src_image = imagecreatefromjpeg($image_from['name']);
            break;
        case IMAGETYPE_GIF:
            $src_image = imagecreatefromgif($image_from);
            break;
        case IMAGETYPE_PNG:
            $src_image = imagecreatefrompng($image_from);
            break;
        default:
            return FALSE;
            break;
    }

    $dest_image = imagecreatetruecolor($width, $height);

    $result = imagecopyresized($dest_image, $src_image,  0,  0,  0,  0, $width, $height, $src_width, $src_height);

    $result = imagegif($dest_image, $image_to);

    return TRUE;
}
    public function resizeImage($_FILES){
        if(isset($_FILES['image'])){
   if($_FILES['image']['size'] < 1){
      echo 'Ошибка загрузки!';
   }
   else if($_FILES['image']['size'] > 1000000){
      echo 'Слишком большой файл!';
   }
   else {
      switch($_FILES['image']['type']){
         case 'image/gif':
         $image = imagecreatefromgif($_FILES['image']['tmp_name']);
         break;
         case 'image/jpeg':
         case 'image/pjpeg':
         $image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
         break;
         case 'image/png':
         $image = imagecreatefrompng($_FILES['image']['tmp_name']);
         break;
      }
      if(!isset($image)){
         echo 'Неверный формат изображения. Допустимы только .gif, .jpg или .png!';
      }else{
          $size = getimagesize($_FILES['image']['tmp_name']);
         $background = imagecreatetruecolor($size[0],$size[1]);
         $white = imagecolorallocate($background,255,255,255);
         imagefill($background,0,0,$white);
         imagecopy($background,$image,0,0,0,0,$size[0],$size[1]);
          $filename = explode(".",$_FILES['image']['name']);
         $image = $background;
         $filename = explode(".",$_FILES['image']['name']);
         $filename = $filename[0];
         imagejpeg($image, '/users/tutors/' . $filename . '.jpg',90);
         imagedestroy($image);
         echo 'Загруженное изображение: <img src="../users/tutors/' . $filename . '.jpg" />';
      }
   }
}
else {
   echo 'Это не изображение!';
}
    }
    public function download($_FILES, $who, $param){
       // var_dump($_FILES); die;
        $destination = 'users' . DS . $who . DS. $param . DS;
        //$fotoname = 'avatar.jpeg';
        if(move_uploaded_file($_FILES['image']['tmp_name'], $destination . $param . '.jpeg')){
            echo 'ok';
        }
    }
    
}